package com.example.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

//统一响应结果
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Result<T> {
    private Integer status;//业务状态码  0-成功  1-失败
    private Boolean success;//操作是否成功（true-成功，false-失败）
    private T data;//响应数据

    //内部类，用于包装code、msg和data
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ResponseData<T> {
        private Integer code; //业务状态码  0-成功  1-失败
        private String msg;  //提示信息
        private T data;      //实际数据
    }

    //快速返回操作成功响应结果(带响应数据)
    public static <E> Result<ResponseData<E>> success(E data) {
        Result<ResponseData<E>> result = new Result<>();
        result.setStatus(0);
        result.setSuccess(true);
        ResponseData<E> responseData = new ResponseData<>();
        responseData.setCode(0);
        responseData.setMsg("操作成功");
        responseData.setData(data);
        result.setData(responseData);
        return result;
    }

    // 新增：支持传入自定义ResponseData的success方法（核心修改）
    public static <E> Result<ResponseData<E>> success(ResponseData<E> responseData) {
        Result<ResponseData<E>> result = new Result<>();
        result.setStatus(0);
        result.setSuccess(true);
        result.setData(responseData); // 直接传入自定义的responseData
        return result;
    }


    //快速返回操作成功响应结果
    public static Result<ResponseData<Object>> success() {
        Result<ResponseData<Object>> result = new Result<>();
        result.setStatus(0);
        result.setSuccess(true);
        ResponseData<Object> responseData = new ResponseData<>();
        responseData.setCode(0);
        responseData.setMsg("操作成功");
        responseData.setData(null);
        result.setData(responseData);
        return result;
    }



    public static Result<ResponseData<Object>> error(String message) {
        Result<ResponseData<Object>> result = new Result<>();
        result.setStatus(1);
        result.setSuccess(false);
        ResponseData<Object> responseData = new ResponseData<>();
        responseData.setCode(1);
        responseData.setMsg(message);
        responseData.setData(null);
        result.setData(responseData);
        return result;
    }
}